package csel.view;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectOutputStream;

import csel.model.*;

public class MapMaker {

	/**
	 * @param args
	 */
	
	/*as of now makes this really simple and ugly map: (X is wall, o is desk, space is floor)

	XXXXXXXX
	X      X
	Xo oo oX
	Xo    oX
	Xo oo oX
	Xo    oX
	XooooooX
	XXXXXXXX
	
	*/
	private static Wall w;
	private static Desk d;
	private static Floor fl;
	private static RealTile[][] tileMatrix;
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("Working Directory = " + System.getProperty("user.dir"));

		w = new Wall();
		d = new Desk();
		fl = new Floor();
		int width = 44;
		int height = 39;
		tileMatrix = new RealTile[width][height];
		File f = new File("csebasement.txt");
		if (f.exists())
			System.out.println("I found the file!");

		Map m = new Map(tileMatrix);
		try
		{
			char oneChar;
			BufferedReader input =  new BufferedReader(new FileReader(f));
			int a = 0,b = 0;
			while ((( oneChar = ((char) input.read())) != '\0') && (b < height))
			{
				RealTile t = new RealTile();
				if (oneChar == 'X')
				{
					t.setTerrain(w);
					t.setLocation(new Location(a,b));
					t.setMap(m);
					tileMatrix[a][b] = t;
					System.out.print("w");
					++a;
				}
				else if (oneChar == 'o')
				{	
					t.setTerrain(d);
					t.setLocation(new Location(a,b));
					t.setMap(m);
					tileMatrix[a][b] = t;
					System.out.print("o");
					++a;
				}
				else if (oneChar == ' ')
				{	
					t.setTerrain(fl);
					t.setLocation(new Location(a,b));
					t.setMap(m);
					tileMatrix[a][b] = t;
					System.out.print(" ");
					++a;
				}
				else if (oneChar == 'n')
				{	
					t = null;
					tileMatrix[a][b] = t;
					System.out.print("n");
					++a;
				}
				
				if (a >= width)
				{
					a = 0;
					++b;
					System.out.println();
				}
			}
		}
		catch (IOException e)
		{
			System.out.println("IO Exception!");
		}
		printThingie();
		try{
		mapSaver(m);
		}
		catch (IOException e)
		{
			System.out.println("IO EXCEPTION!");
		}
	}

public static void mapSaver(Map m) throws FileNotFoundException, IOException
{
	FileOutputStream fileOut = new FileOutputStream("defaultMap");
	ObjectOutputStream serializer = new ObjectOutputStream(fileOut);

	serializer.writeObject(m);
	
	serializer.close();
	fileOut.close();
}
	
public static void printThingie()
{
	int size1 = 39;
	int size2 = 44;
	for (int b = 0; b < size1; ++b)
	{
		for (int a = 0; a < size2; ++a)
		{
			if (tileMatrix[a][b]==null)
				System.out.print("n");
			else if (tileMatrix[a][b].getTerrain()==w)
				System.out.print("X");
			else if (tileMatrix[a][b].getTerrain()==d)
				System.out.print("o");
			else if (tileMatrix[a][b].getTerrain()==fl)
				System.out.print(" ");
			else
				System.out.print("THIS AINT RIGHT!");
		}
		System.out.println();
	}
}

}
